package com.hainatong.jcmvvmbase.login.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.hainatong.jcmvvmbase.R;
import com.hainatong.jcmvvmbase.login.entity.GirlImgBean;

import java.util.List;

public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageHolder>
{
    private Context mContext;

    private List<GirlImgBean> girlImgs;


    public ImageAdapter()
    {
    }

    public ImageAdapter(List<GirlImgBean> girlImgs)
    {
        this.girlImgs = girlImgs;
    }

    public void setGirlImgs(List<GirlImgBean> girlImgs)
    {
        this.girlImgs = girlImgs;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ImageHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
    {
        mContext = parent.getContext();

        return new ImageHolder(LayoutInflater.from(mContext).inflate(R.layout.item_img_view, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ImageHolder holder, int position)
    {
        GirlImgBean girlImg = girlImgs.get( position );
        if( girlImg != null  )
        {
            Glide.with( mContext )
                 .load(  girlImg.getUrl() )
                 .placeholder(R.mipmap.ic_launcher)
                 .error( R.mipmap.ic_launcher )
                 .into( holder.img );
        }
    }


    public GirlImgBean getItem(int position)
    {
        return girlImgs !=null ? girlImgs.get(position) : null ;
    }

    @Override
    public int getItemCount()
    {
        return girlImgs == null ? 0 : girlImgs.size() ;
    }

    class ImageHolder extends RecyclerView.ViewHolder
    {
        private ImageView img;
        public ImageHolder(@NonNull View itemView)
        {
            super(itemView);
            img = itemView.findViewById( R.id.img_view );
        }
    }
}
